<% session.setAttribute("title", "Create Proposal"); %> 

<%@ include file="Header.jsp"%>
<%
	String error = "";
	if (request.getParameter("create") != null) 
		if (request.getParameter("teacher_id").equals("") 
			   || request.getParameter("title").equals("")  
			    || request.getParameter("consultant").equals("")
			     || request.getParameter("proposal_description").equals("")
			      || request.getParameter("technologies").equals("")
			      	|| request.getParameter("execution_time").equals("")
			      		|| session.getAttribute("username").equals("")
		){
			error = "There are errors in your form";
		}
%>
<% if (request.getParameter("create") == null || (request.getParameter("create") != null && !error.equals("")) ) { %>
		<span style='color: red;'> 
			<% if (request.getParameter("create") != null && !error.equals("")) {  out.println( error); } %>
		</span>
		<form action="CreateProposalPage.jsp">
		<table cellpadding="5" cellspacing="5">
			<tr>
				<td>Professor Name:</td>
				<td><input type="text" name="teacher_id" value="<% if (request.getParameter("teacher_id") != null) out.print(request.getParameter("teacher_id"));%>"/></td>
			</tr>
			<tr>
				<td>Title:</td>
				<td><input type="text" name="title" value="<% if (request.getParameter("title") != null) out.print(request.getParameter("title"));%>"/></td>
			<tr>
				<td>Consultant:</td>
				<td><input type="text" name="consultant" value="<% if (request.getParameter("consultant") != null) out.print(request.getParameter("consultant"));%>"/></td>
			<tr>
				<td>Description:</td>
				<td><input type="text" name="proposal_description" value="<% if (request.getParameter("proposal_description") != null) out.print(request.getParameter("proposal_description"));%>"/></td>
			<tr>
				<td>Technologies:</td>
				<td><input type="text" name="technologies" value="<% if (request.getParameter("technologies") != null) out.print(request.getParameter("technologies"));%>"/></td>
			<tr>
				<td>Execution Time</td>
				<td><input type="text" name="execution_time" value="<% if (request.getParameter("execution_time") != null) out.print(request.getParameter("execution_time"));%>"/></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input type="submit" name="create" value="Create Proposal" /> </td>
			</tr>
		</table>
		</form>
 <% } else {
  		org.jbpm.api.ProcessEngine processEngine = StaticProcessEngine.getInstance();

  		RepositoryService repositoryService = processEngine
  				.getRepositoryService();
  		ExecutionService executionService = processEngine
  				.getExecutionService();

  		repositoryService.createDeployment().addResourceFromClasspath(
  				"theses_process.jpdl.xml").deploy();
  		Map<String, Object> variables = new HashMap<String, Object>();
  		variables.put("teacher_id", request.getParameter("teacher_id"));
  		variables.put("student_id", session.getAttribute("username"));
  		variables.put("proposal_id", -1);
  		variables.put("theses_id", -1);
  		variables.put("title", request.getParameter("title"));
  		variables.put("consultant", request.getParameter("consultant"));
  		variables.put("proposal_description", request.getParameter("proposal_description"));
  		variables.put("technologies", request.getParameter("technologies"));
  		variables.put("execution_time",  Integer.valueOf(request.getParameter("execution_time")));

  		executionService.startProcessInstanceByKey("ThesesProcess", variables); %> 
  		The Proposal has been sended 
<% } %> 
 	<br />
<%@ include file="Footer.jsp" %>